using System;
using System.Globalization;
using System.Web;

namespace Smarly.Web.ActionFilters
{
	public static class ResponseCacheExtention
	{
		public static void SetEtag(this HttpResponseBase response, string etag, DateTime lastModify, DateTime expires)
		{
			response.AppendHeader("Last-Modified", lastModify.ToGmt());
			response.AppendHeader("Expires", expires.ToGmt());
			response.AppendHeader("ETag", etag);
		}

		public static void Set304(this HttpResponseBase response, DateTime lastModify, DateTime expires)
		{
			response.AppendHeader("Last-Modified", lastModify.ToGmt());
			response.AppendHeader("Expires", expires.ToGmt());
			response.StatusCode = 304;
			response.TrySkipIisCustomErrors = true;
		}

		public static void ExpireUserEtag(this HttpResponseBase response/*, string etag, DateTime lastModify, DateTime expires*/)
		{
			response.AppendHeader("Last-Modified", DateTime.UtcNow.ToGmt());
			response.AppendHeader("Expires", DateTime.UtcNow.AddYears(-1).ToGmt());
		}

		private static string ToGmt(this DateTime date)
		{
			return date.ToUniversalTime().ToString("ddd, dd MMM yyyy HH:mm:ss 'GMT'", CultureInfo.InvariantCulture);
		}
	}
}